package util;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import jpcap.JpcapCaptor;
import jpcap.JpcapWriter;
import jpcap.NetworkInterface;
import jpcap.NetworkInterfaceAddress;
import jpcap.packet.EthernetPacket;
import jpcap.packet.IPPacket;
import jpcap.packet.Packet;
import jpcap.packet.TCPPacket;

/**
 *
 * @author Huchnet
 */
public class Tools {

    public static Packet createTCPPacket() throws UnknownHostException{

        //open a network interface to send a packet to
        //create a TCP packet with specified port numbers, flags, and other parameters
        TCPPacket p=new TCPPacket(12,34,56,78,false,false,false,false,true,true,true,true,10,10);
        //specify IPv4 header parameters
        p.setIPv4Parameter(0,false,false,false,0,false,false,false,0,1010101,100,IPPacket.IPPROTO_TCP,InetAddress.getByName("www.microsoft.com"),InetAddress.getByName("www.google.com"));
        //set the data field of the packet
        p.data=("data").getBytes();
        //create an Ethernet packet (frame)
        EthernetPacket ether=new EthernetPacket();
        //set frame type as IP
        ether.frametype=EthernetPacket.ETHERTYPE_IP;
        //set source and destination MAC addresses
        ether.src_mac=new byte[]{(byte)0,(byte)1,(byte)2,(byte)3,(byte)4,(byte)5};
        ether.dst_mac=new byte[]{(byte)0,(byte)6,(byte)7,(byte)8,(byte)9,(byte)10};

        //set the datalink frame of the packet p as ether
        p.datalink=ether;

        return p;

    }
    public static void readPackets() throws IOException{
        JpcapCaptor captor=JpcapCaptor.openFile("/Users/Huchnet/Desktop/JpCAP/prueb");

        while(true){
          //read a packet from the opened file
          Packet packet=captor.getPacket();
          //if some error occurred or EOF has reached, break the loop
          if(packet==null || packet==Packet.EOF) break;
          //otherwise, print out the packet
          System.out.println(packet);
        }

        captor.close();
    }
    public static void savePackets(JpcapCaptor captor){

        JpcapWriter writer=null;
        try {
            writer = JpcapWriter.openDumpFile(captor, "/Users/Huchnet/Desktop/JpCAP/prueb");
        } catch (IOException ex) {
            
        }

        for(int i=0;i<10;i++){
          //capture a single packet
          Packet packet=captor.getPacket();
          //save it into the opened file
          writer.writePacket(packet);
        }
        writer.close();
    }

    public static void showInterfaces(){

        NetworkInterface[] devices = JpcapCaptor.getDeviceList();
        for (int i = 0; i < devices.length; i++) {
              //print out its name and description
              System.out.println(i+": "+devices[i].name + "(" + devices[i].description+")");

              //print out its datalink name and description
              System.out.println(" datalink: "+devices[i].datalink_name + "(" + devices[i].datalink_description+")");

              //print out its MAC address
              System.out.print(" MAC address:");
              for (byte b : devices[i].mac_address)
                System.out.print(Integer.toHexString(b&0xff) + ":");
              System.out.println();

              //print out its IP address, subnet mask and broadcast address
              for (NetworkInterfaceAddress a : devices[i].addresses){
                    System.out.println(" address:"+a.address + " " + a.subnet + " "+ a.broadcast);
              }
        }
    }
    
}
